package com.sl.carcode.global;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.WindowManager;
import android.widget.Toast;


import com.alibaba.baichuan.android.trade.AlibcTradeSDK;
import com.alibaba.baichuan.android.trade.callback.AlibcTradeInitCallback;
import com.alibaba.baichuan.trade.biz.core.taoke.AlibcTaokeParams;
import com.baidu.ocr.sdk.OCR;
import com.baidu.ocr.sdk.OnResultListener;
import com.baidu.ocr.sdk.exception.OCRError;
import com.baidu.ocr.sdk.model.AccessToken;
import com.baidu.ocr.sdk.utils.Util;
import com.blankj.utilcode.util.Utils;

import java.util.ArrayList;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * Created by  raysh  on 2017/07/14.
 * Modified by PDY on 2017/11/2.
 */

public class MyApplication extends Application {
    private static Context mContext;
    private static ArrayList<Activity> activityList;
    private static String token;


    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;
        activityList = new ArrayList<>();
//        CrashHandler.getInstance().init(this);
        //
        Utils.init(this);

        OCR.getInstance().initAccessToken(new OnResultListener<AccessToken>() {
            @Override
            public void onResult(AccessToken result) {
                // 调用成功，返回AccessToken对象
                token = result.getAccessToken();
//                Toast.makeText(getContext(), token + "to", Toast.LENGTH_SHORT).show();
                Log.i("sl", token + "token");
            }

            @Override
            public void onError(OCRError error) {
                // 调用失败，返回OCRError子类SDKError对象
            }
        }, getApplicationContext());
        AlibcTradeSDK.asyncInit(this, new AlibcTradeInitCallback() {
            @Override
            public void onSuccess() {
                // 初始化成功，设置相关的全局配置参数
                // 是否使用支付宝
                AlibcTradeSDK.setShouldUseAlipay(true);
                // 设置是否使用同步淘客打点
                AlibcTradeSDK.setSyncForTaoke(true);
                // 是否走强制H5的逻辑，为true时全部页面均为H5打开
                AlibcTradeSDK.setForceH5(true);
                // 设置全局淘客参数，方便开发者用同一个淘客参数，不需要在show接口重复传入
                AlibcTaokeParams alibcTaokeParams = new AlibcTaokeParams();
                alibcTaokeParams.setPid("mm_128779697_0_0");
                AlibcTradeSDK.setTaokeParams(alibcTaokeParams);

//                System.out.println(rsp.getBody());
//
            }

            @Override
            public void onFailure(int code, String msg) {
                Toast.makeText(MyApplication.this, "初始化失败,错误码=" + code + " / 错误消息=" + msg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    public static String getToken() {
        return token;
    }

    /**
     * 添加Activity
     *
     * @param activity
     */
    public static void addActivity(Activity activity) {
        activityList.add(activity);
    }

    /**
     * 退出所有activity
     */
    public static void finishAll() {
        for (Activity activity : activityList) {
            activity.finish();
        }
    }

    /**
     * 获取Context
     *
     * @return
     */
    public static Context getContext() {
        return mContext;
    }

    /**
     * 根据id获取String数组
     *
     * @param id String[]的id
     * @return
     */
    public static String[] getStringArray(int id) {
        return getContext().getResources().getStringArray(id);
    }

    /**
     * 获取应用版本号
     *
     * @return
     */
    public static int getAPPVersion() {
        PackageManager packageManager = getContext().getPackageManager();
        TelephonyManager telephonyManager = (TelephonyManager) getContext().getSystemService(TELEPHONY_SERVICE);
        int version = 0;
        try {
            PackageInfo packageInfo = packageManager.getPackageInfo(getContext().getPackageName(), 0);
            version = packageInfo.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return version;
    }

    /**
     * 获取应用版本号
     *
     * @return
     */
    public static String getAPPVersionName() {
        PackageManager packageManager = getContext().getPackageManager();
        TelephonyManager telephonyManager = (TelephonyManager) getContext().getSystemService(TELEPHONY_SERVICE);
        String versionName = "";
        try {
            PackageInfo packageInfo = packageManager.getPackageInfo(getContext().getPackageName(), 0);
            versionName = packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionName;
    }

    /**
     * 获取当前线程ID
     *
     * @return 线程ID
     */
    public static int getThreadId() {
        return android.os.Process.myTid();
    }

    /**
     * 获取当前进程ID
     *
     * @return 进程ID
     */
    public static int getProcessId() {
        return android.os.Process.myPid();
    }

    /**
     * 判断当前线程是否是UI线程
     *
     * @param id
     * @return
     */
    public static boolean isRunOnUiThread(int id) {
        return id == getThreadId();
    }

    public static int dip2px(float dpValue) {
        final float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * sp转换成px
     *
     * @param spValue
     * @return
     */
    public static int sp2px(float spValue) {
        final float fontScale = getContext().getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    /**
     * px转换成sp
     *
     * @return
     */
    public static int px2sp(float pxValue) {
        final float fontScale = getContext().getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }

    public static int getWindowWidth() {
        WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        return width;
    }

    public static int getWindowHeight() {
        WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        int height = wm.getDefaultDisplay().getHeight();
        return height;
    }

}
